<% layout('/layouts/default.html', {title: '销售单管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(stSaleBill.isNewRecord ? '新增销售单' : '编辑销售单')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${stSaleBill}" action="${ctx}/st/stSaleBill/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="id"/>
				<div class="row">
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('单据号')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="billNo" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('单据日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="billDate" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="datetime" data-type="datetime" data-format="yyyy-MM-dd HH:mm"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('销/退货')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="saleOrNot" dictType="st_sale_or_not" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('客户')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="customId" items="${customList}"  itemLabel="companyFullName" itemValue="id" blankOption="true" class="form-control" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('销售类型')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="saleType" dictType="st_sale_type" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('生产状态')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:select path="productStatus" dictType="st_product_status" blankOption="true" class="form-control digits" />-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('合同编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="contractNo" maxlength="20" class="form-control"/>
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('业务员编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="bussinessManCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('业务员')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:treeselect id="businessMan" title="业务员选择" path="bussinessManCode" labelPath="businessMan"
								url="${ctx}/sys/office/treeData?isLoadUser=true" class="" allowClear="true"/>
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('部门编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="deptCode" maxlength="20" class="form-control"/>-->
<!--								-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('部门')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:treeselect id="testOffice" title="部门选择"
								path="deptCode" labelPath="deptName"
								url="${ctx}/sys/office/treeData" class="" allowClear="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('币别')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="moneyType" dictType="st_money_type" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('金额')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="money" class="form-control number"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('收款账期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="receipt" maxlength="36" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('项目归属')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="projectBelong" maxlength="20" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('地址')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="address" maxlength="40" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('是否做业绩考核')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="examine" dictType="st_is_examine" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('交货日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="deliveryDate" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="datetime" data-type="datetime" data-format="yyyy-MM-dd HH:mm"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注信息')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-unit">${text('销售单明细')}</h4>
				<div class="ml10 mr10">
					<table id="stSaleBillDetailDataGrid"></table>
					<% if (hasPermi('st:stSaleBill:edit')){ %>
						<a href="#" id="stSaleBillDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('st:stSaleBill:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
//初始化销售单明细DataGrid对象
$("#stSaleBillDetailDataGrid").dataGrid({

	data: ${toJson(stSaleBill.stSaleBillDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	
	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'id', editable:true, hidden:true},
		{header:'${text("产品名称")}', name:'productName', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{productName:'&nbsp;',id:''}], ${productList}),
				itemLabel: 'productName', itemValue: 'id', dataInit: function(element) {
					$(element).select2().on("change", function (e) {
						$(this).resetValid()
					});
					$(element).on("select2:select",function(e){
						var data = e.params.data;
						if('' ==data.id) return;
						var productList=${productList};
						var chooseItem=null;
						for(var index in productList){
							var item =productList[index];
							if(productList[index].id== data.id){
								chooseItem=item;
								break;
							}
						}
						console.info("product",productList);
						// var td_1 = $(($(($("#stInventoryBillDetailDataGrid").find("tr"))[parseInt(index) + 1+0]).find('td'))[3]);//前面3行都什么啊
						var formula = $(element).closest('td').next();
						var f_s=formula.find('.form-control');
						$.ajax({
							url:"${ctx}/st/formula/stFormulaName/id",
							dataType:'json',
							type:'POST',
							data:{
								//请求参数
								productId:chooseItem.productId
							},
							success:function(result){
								if(1!=result.code){
									js.showMessage("请联系管理员");
									return;
								}
								f_s.empty();
								for(var i=0;i<result.list.length;i++){
									var item =result.list[i];
									var op='<option value="'+item.id+'">'+item.code+'</option>';
									f_s.append(op);
								}
								f_s.change();
							}
						});

						// var op='<option value="'+k+'">'+v+'</option>';

						var unit=formula.next();
						unit.find('.form-control').val('11');
						// unit.attr("readonly", true);
						var number=unit.next();
						number.find('.form-control').val('11');
						var rate = number.next();
						rate.find('.form-control').val('2020-11-26 10:10:10');
						var singlePrice = rate.next();
						singlePrice.find('.form-control').val(chooseItem.referenceInPrice);
						var referencePrice = singlePrice.next();
						referencePrice.find('.form-control').val(chooseItem.referenceOutPrice);
						// referencePrice.attr("readonly", true);
						var money = referencePrice.next();
						money.find('.form-control').val('0');
						// money.attr("readonly", true);
					});
				}
			}
		},
		{header:'${text("生产配方")}', name:'productFormula', width:150,
			editable:true, edittype:'select', editoptions: {
				'class': 'form-control',
				items: $.merge([{code: '&nbsp;', id: ''}], ${formulaList}),
				itemLabel: 'code', itemValue: 'id', dataInit: function (element) {
					$(element).select2().on("change", function (e) {
						$(this).resetValid()
					});
				}
			}
		},
		{header:'${text("单位")}', name:'unit', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'36', 'class':'form-control','readonly':'true'}},
		{header:'${text("数量")}', name:'number', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'8', 'class':'form-control digits'}},
		{header:'${text("税率")}', name:'rate', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'36', 'class':'form-control'}},
		{header:'${text("单价")}', name:'singlePrice', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("参考售价")}', name:'referencePrice', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number','readonly':'true'}},
		{header:'${text("金额")}', name:'money', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number','readonly':'true'}},
		{header:'${text("优惠金额")}', name:'discountPrice', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("备货日期")}', name:'prepareDate', width:150, 
			formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},
			editable:true, edittype:'text', editoptions:{'class':'form-control laydate', 'readonly':'true',
				dataInit: function(element){
					laydate.render({elem:element, type:'datetime', format:'yyyy-MM-dd HH:mm'});
				}
			}
		},
		{header:'${text("备注")}', name:'ramarks', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'500', 'class':'form-control'}},
		{header:'${text("父id")}', name:'parentId.id', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stSaleBillDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stSaleBillDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],
	
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#stSaleBillDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'stSaleBillDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,id,productName,productFormula,unit,number,rate,singlePrice,referencePrice,money,discountPrice,prepareDate,ramarks,parentId.id,', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});
</script>